<template><el-dialog :title="!dataForm.id ? '入住登记' : '信息编辑'" :close-on-click-modal="false" :visible.sync="visible">
  <el-form label-position="right" size="small" inline="true" :model="dataForm" :rules="dataRule" ref="dataForm"
    @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="客户姓名" prop="customername">
      <el-input v-model="dataForm.customername" placeholder="请输入客户姓名"></el-input>
    </el-form-item>
    <el-form-item label="楼栋" prop="buildingno">
      <el-input v-model="buildingno" placeholder="所属楼房"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="customerage">
      <el-input v-model="dataForm.customerage" placeholder="年龄"></el-input>
    </el-form-item>
    <el-form-item label="新房间" prop="newRoomNo">
      <el-select v-model="dataForm.newRoomNo" @change="roomchange" placeholder="请选择新房间">
        <el-option v-for="item in roomList" :key="item.id" :label="item.roomno" :value="item.roomno">
        </el-option>
      </el-select>
    </el-form-item>
    <!-- <el-form-item label="性别" prop="customersex">
      <el-input v-model="dataForm.customersex" placeholder="性别"></el-input>
    </el-form-item> -->
    <el-form-item label="性别">
      <el-select v-model="dataForm.customersex" placeholder="请选择">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="新床位" prop="newBedId">
      <el-select v-model="dataForm.newBedId" @change="bedchange" placeholder="请选择新床位">
        <el-option v-for="item in bedList" :key="item.id" :label="item.bedno" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="出生日期" prop="startdate">
      <el-date-picker v-model="dataForm.startdate" type="date" placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="入住时间" prop="checkindate">
      <el-date-picker v-model="dataForm.checkindate" type="date" placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="身份证号" prop="idcard">
      <el-input v-model="dataForm.idcard" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="合同到期时间" prop="expirationdate">
      <el-date-picker v-model="dataForm.expirationdate" type="date" placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="血型" prop="bloodtype">
      <el-input v-model="dataForm.bloodtype" placeholder="血型"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="contacttel">
      <el-input v-model="dataForm.contacttel" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="家属" prop="familymember">
      <el-input v-model="dataForm.familymember" placeholder="家属"></el-input>
    </el-form-item>
    <el-form-item label="身心状况" prop="psychosomaticstate">
      <el-input v-model="dataForm.psychosomaticstate" placeholder="身心状况"></el-input>
    </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="visible = false">取消</el-button>
    <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
  </span>
</el-dialog></template>

<script>
export default {
  data() {
    return {
      options: [{
        value: '0',
        label: '男'
      }, {
        value: '1',
        label: '女'
      },],
      pageIndex: 1,
      pageSize: 100,
      newRoomNo: '',
      newBedId: '',
      buildingno: '606',
      visible: false,
      dataForm: {
        id: 0,
        isdeleted: '',
        customername: '',
        customerage: '',
        customersex: '男',
        idcard: '',
        roomno: '',
        checkindate: '',
        expirationdate: '',
        contacttel: '',
        bedid: '',
        psychosomaticstate: '',
        attention: '',
        birthday: '',
        height: '',
        weight: '',
        bloodtype: '',
        filepath: '',
        userid: '',
        levelid: '',
        familymember: '',
        startdate: '',
        newBedNo: '',
        newBedId: '',
      },
      roomList: [],//房间列表
      bedList: [],//床位列表
      dataRule: {
        isdeleted: [
          { required: true, message: '逻辑删除标记（0：显示；1：隐藏）不能为空', trigger: 'blur' }
        ],
        customername: [
          { required: true, message: '客户姓名不能为空', trigger: 'blur' }
        ],
        customerage: [
          { required: true, message: '年龄不能为空', trigger: 'blur' }
        ],
        customersex: [
          { required: true, message: '性别  0：男  1：女不能为空', trigger: 'blur' }
        ],
        idcard: [
          { required: true, message: '身份证号不能为空', trigger: 'blur' }
        ],
        roomno: [
          { required: true, message: '房间号不能为空', trigger: 'blur' }
        ],
        buildingno: [
          { required: true, message: '所属楼房不能为空', trigger: 'blur' }
        ],
        checkindate: [
          { required: true, message: '入住时间不能为空', trigger: 'blur' }
        ],
        expirationdate: [
          { required: true, message: '合同到期时间不能为空', trigger: 'blur' }
        ],
        contacttel: [
          { required: true, message: '联系电话不能为空', trigger: 'blur' }
        ],
        bedid: [
          { required: true, message: '床号不能为空', trigger: 'blur' }
        ],
        psychosomaticstate: [
          { required: true, message: '身心状况不能为空', trigger: 'blur' }
        ],
        attention: [
          { required: true, message: '注意事项不能为空', trigger: 'blur' }
        ],
        birthday: [
          { required: true, message: '出生日期不能为空', trigger: 'blur' }
        ],
        height: [
          { required: true, message: '身高不能为空', trigger: 'blur' }
        ],
        weight: [
          { required: true, message: '体重不能为空', trigger: 'blur' }
        ],
        bloodtype: [
          { required: true, message: '血型不能为空', trigger: 'blur' }
        ],
        filepath: [
          { required: true, message: '头像路径不能为空', trigger: 'blur' }
        ],
        userid: [
          { required: true, message: '关联系统健康管家(护工)  无管家设置  -1不能为空', trigger: 'blur' }
        ],
        levelid: [
          { required: true, message: '护理等级不能为空', trigger: 'blur' }
        ],
        familymember: [
          { required: true, message: '家属不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getRoomList();
  },
  methods: {
    roomchange(event) {
      this.getBedListByRoomNo(event)
    },
    getBedListByRoomNo(newRoomNo) {
      //清空bedList
      this.bedList = [];
      //清空newBedNo
      this.dataForm.newBedId = '';
      this.$http({
        url: this.$http.adornUrl(`/bed/bed/bedList/${newRoomNo}`),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.bedList = data.bedList;
          console.log(this.bedList);
        }
      })
    },
    //获取房间列表
    getRoomList() {
      this.$http({
        url: this.$http.adornUrl(`/room/room/list1/`),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key,
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.roomList = data.page.list;
          console.log(this.roomList);
        }
      })
    },
    init(id) {
      // getRoomList()
      this.dataForm.id = id || 0
      this.visible = true
      // getRoomList()
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/customer/customer/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.isdeleted = data.customer.isdeleted
              this.dataForm.customername = data.customer.customername
              this.dataForm.customerage = data.customer.customerage
              this.dataForm.customersex = data.customer.customersex
              this.dataForm.idcard = data.customer.idcard
              this.dataForm.roomno = data.customer.roomno
              this.dataForm.buildingno = data.customer.buildingno
              this.dataForm.checkindate = data.customer.checkindate
              this.dataForm.expirationdate = data.customer.expirationdate
              this.dataForm.contacttel = data.customer.contacttel
              this.dataForm.bedid = data.customer.bedid
              this.dataForm.psychosomaticstate = data.customer.psychosomaticstate
              this.dataForm.attention = data.customer.attention
              this.dataForm.birthday = data.customer.birthday
              this.dataForm.height = data.customer.height
              this.dataForm.weight = data.customer.weight
              this.dataForm.bloodtype = data.customer.bloodtype
              this.dataForm.filepath = data.customer.filepath
              this.dataForm.userid = data.customer.userid
              this.dataForm.levelid = data.customer.levelid
              this.dataForm.familymember = data.customer.familymember
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/customer/customer/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'isdeleted': this.dataForm.isdeleted,
              'customername': this.dataForm.customername,
              'customerage': this.dataForm.customerage,
              'customersex': this.dataForm.customersex,
              'idcard': this.dataForm.idcard,
              'roomno': this.dataForm.roomno,
              'buildingno': this.dataForm.buildingno,
              'checkindate': this.dataForm.checkindate,
              'expirationdate': this.dataForm.expirationdate,
              'contacttel': this.dataForm.contacttel,
              'bedid': this.dataForm.bedid,
              'psychosomaticstate': this.dataForm.psychosomaticstate,
              'attention': this.dataForm.attention,
              'birthday': this.dataForm.birthday,
              'height': this.dataForm.height,
              'weight': this.dataForm.weight,
              'bloodtype': this.dataForm.bloodtype,
              'filepath': this.dataForm.filepath,
              'userid': this.dataForm.userid,
              'levelid': this.dataForm.levelid,
              'familymember': this.dataForm.familymember
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
